import 'package:flutter/material.dart';

class DargMoveWidget extends StatefulWidget {
  final Offset offset;
  final Color color;
  const DargMoveWidget({Key key, this.offset, this.color}) : super(key: key);
  @override
  _DargWidgetState createState() => _DargWidgetState();
}

class _DargWidgetState extends State<DargMoveWidget> {
  Offset offset = Offset(0.0, 0.0);

  @override
  void initState() {
    super.initState();
    offset = widget.offset; // 取得传进来的offset
  }

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: this.offset.dx,
      top: this.offset.dy,
      child: Draggable(
        data: widget.color, // 给拖拽接收器用的
        child: Container(
          height: 100.0,
          width: 100.0,
          color: widget.color,
        ),
        feedback: Container(
          width: 100.0,
          height: 100.0,
          color: widget.color.withOpacity(0.5),
        ), // 拖动时的样式
        onDraggableCanceled: (Velocity velocity, Offset offset) {
          print(offset);
          setState(() {
            this.offset = offset; // 更新盒子的位置
          });
        }, // 拖拽的回调
      ),
    );
  }
}
